//
//  JRVisitorVIew.swift
//  myWeibo
//
//  Created by 樊健荣 on 16/7/18.
//  Copyright © 2016年 樊健荣. All rights reserved.
//

import UIKit


protocol JRVisitorVIewDelegate: NSObjectProtocol {
    func visitorVIewRegister() -> Void
    func visitorVIewLogin() -> Void
}

class JRVisitorVIew: UIView {

    ///设置代理属性
    weak var delegate: JRVisitorVIewDelegate?
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        //加载访客View子控件
        loadSubView()
        
    }
    
    // MARK: - 配置外部方法
    ///根据控制器设置图片,文字
    func setupUIInfo (imageName: String, title: String) -> Void {
        //先将房子ImageV隐藏
        self.houseImageV.hidden = true
        //将遮罩放到最底层
        self.sendSubviewToBack(shadowImageV)
        //根据传入的数据设置页面
        self.iconImageV.image = UIImage(named: imageName)
        self.titleLabel.text = title
        
        
    }
    ///首页圆圈动画
    func animationStart () -> Void {
        //创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        self.iconImageV.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    
    //加载子控件
    private func loadSubView () -> Void {
        
        //将懒加载空间加载到父控件
        self.addSubview(iconImageV)
        self.addSubview(shadowImageV)
        self.addSubview(houseImageV)
        self.addSubview(titleLabel)
        self.addSubview(registerBtn)
        self.addSubview(loginBtn)
        
        
        
        // 设置约束
        // 使用纯代码约束需要关闭Autoresizing的影响
        iconImageV.translatesAutoresizingMaskIntoConstraints = false
        shadowImageV.translatesAutoresizingMaskIntoConstraints = false
        houseImageV.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //约束
        // 将约束添加到view上面
        self.addConstraint(NSLayoutConstraint(item: iconImageV, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        /// CenterY和父控件的CenterY重合往上面偏移40
        self.addConstraint(NSLayoutConstraint(item: iconImageV, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        ///遮罩
        // 往父控件添加约束 CenterX
        self.addConstraint(NSLayoutConstraint(item: shadowImageV, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageV, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 遮罩 CenterY
        self.addConstraint(NSLayoutConstraint(item: shadowImageV, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImageV, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /// 房子 CenterX
        // 往父控件添加约束
        self.addConstraint(NSLayoutConstraint(item: houseImageV, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageV, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 房子 CenterY
        self.addConstraint(NSLayoutConstraint(item: houseImageV, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImageV, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /// 消息label CenterX
        self.addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 消息label Top到iconImageV底部
        self.addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImageV, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 消息label 宽度
        // 当 toItem = nil, attribute = NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        
        // 注册按钮
        // 按照文字的书写顺序: Leading, Trailing
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        
        
        
        
    }
    
    // MARK: - 按钮点击事件
    func registerClick() {
        self.delegate?.visitorVIewRegister()
    }
    func loginClick() {
        self.delegate?.visitorVIewLogin()
    }
    
    
    
    // MARK : - 懒加载子控件
    //圆圈
    lazy var iconImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    lazy var shadowImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //房子
    lazy var houseImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字
    lazy var titleLabel: UILabel = {
        let title = UILabel(frame: CGRectZero)
        title.text = "关注一些人,看看有什么惊喜!"
        title.numberOfLines = 0
        title.font = UIFont.systemFontOfSize(18)
        title.textColor = UIColor.darkTextColor()
        self.addSubview(title)
        title.sizeToFit()
        
        return title
    }()
    
    //注册
    lazy var registerBtn: UIButton = {
        let register: UIButton = UIButton(type: UIButtonType.Custom)
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        register.setTitle("注册", forState: UIControlState.Normal)
        register.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        register.titleLabel?.font = UIFont.systemFontOfSize(18)
        register.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return register
    }()
    
    //登录
    lazy var loginBtn: UIButton = {
        
        let login: UIButton = UIButton(type: UIButtonType.Custom)
        login.setTitle("登录", forState: UIControlState.Normal)
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        login.titleLabel?.font = UIFont.systemFontOfSize(18)
        login.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        login.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return login
    }()
}
